package com.resource.controller;

import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

import javax.annotation.Resource;

import com.resource.utils.ResultBody;
import com.resource.utils.ServiceResultHandler;
import org.springframework.web.bind.annotation.*;

import com.resource.dto.CustomerSaveDto;
import com.resource.query.CustomerQuery;
import com.resource.service.CustomerService;

/**
 * 客户管理
 * 
 * @Author: xiongbo.wan
 * @Date: 14:16 2020/9/22 0022
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {

	@Resource
	private CustomerService customerService;

	/**
	 * [新增]
	 **/
	@PostMapping
	public ResultBody create(CustomerSaveDto dto) {
		return ServiceResultHandler.process((Consumer<Object>) (o) -> customerService.create((CustomerSaveDto) o), dto);
	}

	/**
	 * [刪除]
	 **/
	@DeleteMapping("/{id}")
	public ResultBody remove(@PathVariable Long id) {
		return ServiceResultHandler.process((Consumer<Object>) (o) -> customerService.remove((Long) o), id);
	}

	/**
	 * [更新]
	 **/
	@PutMapping("/{id}")
	public ResultBody modify(CustomerSaveDto dto, @PathVariable String id) {
		return ServiceResultHandler.process(
				(BiFunction<Object, Object, Object>) (o, b) -> customerService.modify((CustomerSaveDto) o, (String) b),
				dto, id);
	}

	/**
	 * [查询] 根据主键 id 查询
	 **/
	@GetMapping("/{id}")
	public ResultBody load(@PathVariable Long id) {
		return ServiceResultHandler.process((Function<Object, Object>) o -> customerService.load((Long) o), id);
	}

	/**
	 * [查询] 不分页查询
	 **/
	@GetMapping("/all")
	public ResultBody list(CustomerQuery customerQuery) {
		return ServiceResultHandler.process((Function<Object, Object>) o -> customerService.list((CustomerQuery) o),
				customerQuery);
	}

}
